<?php

/**
 * 
 * @author Administrator
 *
 */
class ServerController extends ApiBaseController {

	/**
	 * 申请服务器通过时运维方所调用的接口
	 * 向后台增加一条服务器信息
	 */
    public function addServer(){  	
    	$json_data = PubRequest::R('json_data');
    	$data = json_decode($json_data,true);
    	
    	//检查必须参数是否完整
    	if(!isset($data['id']) || !isset($data['server_name']) || !isset($data['develop_id']) || !isset($data['oss_api']) || !isset($data['log_server']) 
    			 || !isset($data['open_time']) || !isset($data['status']) || !isset($data['channel_list']) || !isset($data['special_id']) || !isset($data['server_num'])
    			|| !isset($data['port']) || !isset($data['ip']) || !isset($data['name']) || !isset($data['sort_num'])){
    		$json_arr = array('status'=>-1,'msg'=>'参数不完整','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}
    	
    	$develop_id = $data['develop_id'];
    	$open_time = $data['open_time'];
    	$port = $data['port'];
    	$ip = $data['ip'];
    	$name = $data['name'];
    	$server_num = $data['server_num'];
    	$server_name = $data['server_name'];
    	
    	//检查获取的数据与申请的数据是否一致
    	$modServerApply = new ModServerApply();
    	$info = $modServerApply->findByPk($data['id'])->getAttributes();
    	if(!$info){
    		$json_arr = array('status'=>-1,'msg'=>'申请数据不存在','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	} 
    	
    	//检查develop_id是否已经存在
    	$modServer = new ModServer();
    	$count = $modServer->repeat(0, 'develop_id', $data['develop_id'],'mango');
    	if($count > 0){
    		$json_arr = array('status'=>-1,'msg'=>'develop_id已经存在','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}
    	
//     	$count_num = $modServer->repeat(0, 'server_num', $data['server_num'],'mango');
//     	if($count_num > 0){
//     		$json_arr = array('status'=>-1,'msg'=>'服ID已经存在','data'=>'' );
//     		ApiBaseService::doLog($_POST, $json_arr);
//     		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
//     	}
    	
//     	$count_name = $modServer->repeat(0, 'name', $data['name'],'mango');
//     	if($count_name > 0){
//     		$json_arr = array('status'=>-1,'msg'=>'游戏结点名已经存在','data'=>'' );
//     		ApiBaseService::doLog($_POST, $json_arr);
//     		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
//     	}

    	//检查专服是否存在
    	$modSpecialServer = new ModSpecialServer();
    	$special_obj = $modSpecialServer->findByAttributes(array('special_id'=>$data['special_id'],'game'=>'mango'));
    	if(!$special_obj){
    		$json_arr = array('status'=>-1,'msg'=>'后台不存在该专服','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}

    	$api_synchro_url = $special_obj->api_synchro_url;  //同步URL    	
    	if($api_synchro_url == ''){
    		$json_arr = array('status'=>-1,'msg'=>'专服未设置同步URL','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}
    	
    	//检查渠道是否存在
    	$modChannel = new ModChannel();
    	$channel_array = explode(',', $data['channel_list']);
    	$channel_list = '';
    	foreach ($channel_array as $key=>$val){
    		$channel_list .= ','.'"'.$val.'"';
    	}
    	$channel_list = substr($channel_list, 1);
    	$channel = $modChannel->getTabCount($channel_list);
    	if(count($channel) != count($channel_array)){
    		$json_arr = array('status'=>-1,'msg'=>'渠道列表存在后台不存在的数据','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}
    	
    	//入库
    	$modServer = new ModServer();
    	$modServer->platform      = 'test';
    	$modServer->game          = 'mango';
    	$modServer->server_num    = $data['server_num'];
    	$modServer->server_name   = $data['server_name'];
    	$modServer->develop_id    = $data['develop_id'];
    	$modServer->oss_api       = $data['oss_api'];
    	$modServer->log_server    = $data['log_server'];
    	$modServer->ip            = $data['ip'];
    	$modServer->port          = $data['port'];
    	$modServer->open_time     = $data['open_time'];
    	$modServer->union_time    = $data['union_time'];
    	$modServer->union_to      = $data['union_server'];
    	$modServer->check_flag    = $data['status'];
    	$modServer->status        = $data['status'];//废弃字段
    	$modServer->name        = $data['name'];
    	
    	$modServer->show_status        = 1;
    	$modServer->float_status        = 1;
    	$modServer->sort_num        = $data['sort_num'];
    	
    	$modServer->create_user   = 1;
    	$modServer->create_time   = time();
    	$modServer->update_user   = 1;
    	$modServer->update_time   = time();
    	$result = $modServer->save();
    	
    	if($result !== false){
    		//记录服务器与渠道的关系
    		$modServerChannel = new ModServerChannel();
    		$channel_data = array();
    		foreach ($channel as $key=>$val){
    			$data = array();
    			$data['channel_id'] = $val['id'];
    			$data['server_id'] = $modServer->id;
    			array_push($channel_data, $data);
    		}
    		$modServerChannel->addServerChannel($channel_data);
    		//记录服务器与渠道的关系
    		 
    		//记录服务器与专服的关系
    		$modSpecialServicer = new ModSpecialServicer();
    		$modSpecialServicer->special_id = $special_obj->id;
    		$modSpecialServicer->server_id = $modServer->id;
    		$modSpecialServicer->save();
    		//记录服务器与专服的关系
    		
    		//同步游戏服务器的配置
    		$synchro = array();
    		$synchro['name'] = $name;
    		$synchro['show_status'] = 1;
    		$synchro['ip'] = $ip;
    		$synchro['port'] = $port;
    		$synchro['float_status'] = 1;
    		$synchro['open_time'] = $open_time;
    		$synchro['show_name'] = $server_name;
    		$synchro['server_id'] = $server_num;
    		$api = new ApiSynchro();
    		$result = $api->gameAdd($synchro,$api_synchro_url);
    		if($result['state']){ //同步成功
    			//同步服务器与渠道的配置关系
    			$game = 'mango';
    			self::channel_syn($channel,$game,$modServer->id,2,$api_synchro_url);
    		
    		}else{
    			//停止同步操作
    			$update = array();
    			$update['is_syn'] = 2;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($modServer->id, $update);
    		}
    		
    		$json_arr = array('status'=>1,'msg'=>'添加成功','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}else{
    		$json_arr = array('status'=>-1,'msg'=>'添加失败','data'=>'' );
    		ApiBaseService::doLog($_POST, $json_arr);
    		exit(json_encode($json_arr,JSON_UNESCAPED_UNICODE));
    	}	
    	
    }
    
    
    public static function channel_syn($channel_array,$game,$c_id,$is_syn,$api_synchro_url){
    	//同步服务器与渠道的配置关系
    	$api = new ApiSynchro();
    	$modServer = new ModServer();
    	$modServerChannel = new ModServerChannel();
    	foreach ($channel_array as $key=>$val){
    		$channel_id = $val['id'];
    		$gameList = $modServerChannel->selectServerForChannel1($channel_id,$game,$api_synchro_url);
    		$channelConfig = array();
    		 
    		foreach ($gameList as $gkey=>$gval){
    			$channelConfig['channel_id'] = $gval['tab'];
    			$channelConfig['game_list'][] = $gval['name'];
    		}
    		
    		//echo $channelConfig['channel_id'];
    		//先查询channel_id是否已经存在，存在则修改，不存在则添加
    		$isresult = $api->channelGet($channelConfig['channel_id'],$api_synchro_url);
    		if($isresult['state']){
    			if(count($isresult['data']) == 0){ //添加
    				$aeresult = $api->channelAdd($channelConfig,$api_synchro_url);
    			}else{ //编辑
    				$aeresult = $api->channelEdit($channelConfig,$api_synchro_url);
    			}
    			 
    			$update = array();
    			$update['syn_time'] = time();
    			if(!$aeresult['state']){
    				$update['is_syn'] = $is_syn;
    			}else{
    				$update['is_syn'] = 1;
    			}
    			$modServer->updateByPk($c_id, $update);
    		}else{
    			//停止同步
    			$update = array();
    			$update['is_syn'] = $is_syn;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($c_id, $update);
    		}
    	}
    }
    
    /**
     * 该接口为游戏内部测试下载更新包的链接
     * http://admin.s10.q-dazzle.com:8090/api/?r=server/download&plat=android&channel=0&version=1
     * http://192.168.1.10:8090/api/?r=server/download&plat=android&channel=100&version=1
     */
    public function download(){
    	$plat = PubRequest::R('plat');
    	$channel = PubRequest::R('channel');
    	$version = PubRequest::R('version');
    	
    	if($channel == ""){
    		echo "[缺少参数 : 渠道]";
    		exit;
    	}

    	if($plat == ""){
    		echo "[缺少参数 : 平台]";
    		exit;
    	}
    	
    	if($version == ""){
    		echo "[缺少参数 : 资源版本]";
    		exit;
    	}
    	
    	$modResource = new ModResource();
    	$condition = array();
    	$condition['g'] = 'mango';
    	$condition['platform'] = $plat;
    	$condition['channel'] = $channel;
    	$condition['v_num'] = $version;
    	//$condition['is_valid'] = 2; //有效
    	$list = $modResource->select($condition);
//     	echo '<pre/>';
//     	var_dump($list);exit;
    	
    	if(count($list) == 0){
    		echo "[找不到所需的记录]";
    		exit;
    	}
    	
    	$c_version = 0;
    	$s_version = 0;
    	$download_path = "";
    	$apk_path = "";
    	
    	foreach ($list as $key=>$val){
    		if($val['v_num'] > $c_version){
    			$c_version = $val['v_num'];
    			$s_version = $val['s_version'];
    			$download_path = $val['download_path'];
    		}
    		
    		if($val['v_num'] > $version &&  $val['apk'] != '' )
    		{
    			$version = $val['v_num'];
    			$apk_path = $val['apk'];
    		}	
    	}
    	
    	if($apk_path != "") $apk_path = "|".$apk_path;
    	
    	if($c_version != 0 && $s_version != 0 && $download_path != ""){
    		echo $c_version."|".$s_version."|".$download_path.$apk_path;
    		exit;
    	}
    	


    }
    
   

}